package com.zb.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
import com.zb.constant.CodeConstant;

import java.util.Objects;

/**
 * @Author WEN
 * @Date 2023/9/9 0009
 **/
public class CodeStoreStatusIntegerToStringConverter implements Converter<Integer> {
    private static final String CODE_STORE_STATUS_1 = "未出入库";
    private static final String CODE_STORE_STATUS_2 = "预入库";
    private static final String CODE_STORE_STATUS_3 = "已入库";
    private static final String CODE_STORE_STATUS_4 = "预出库";
    private static final String CODE_STORE_STATUS_5 = "已出库";

    private static final String CODE_STATUS_1 = "未质保"; // 编码状态：1-未质保，2-质保中、3-已过保，4-已作废
    private static final String CODE_STATUS_2 = "质保中"; // 编码状态：1-未质保，2-质保中、3-已过保，4-已作废
    private static final String CODE_STATUS_3 = "已过保"; // 编码状态：1-未质保，2-质保中、3-已过保，4-已作废
    private static final String CODE_STATUS_4 = "已作废"; // 编码状态：1-未质保，2-质保中、3-已过保，4-已作废

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return  NumberUtils.parseInteger(cellData.getStringValue(), contentProperty);
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String val = "";
        String fileName = contentProperty.getField().getName();
        // 商品类型
        if (Objects.equals("type", fileName)) {
            if (Objects.equals(1, value)) {
                return new WriteCellData<>("成品");
            }
            if (Objects.equals(2, value)) {
                return new WriteCellData<>("备件");
            }
        }

        // 编码状态
        if (Objects.equals("codeStatus", fileName)) {
            if (Objects.equals(CodeConstant.CODE_STATUS_1, value)) {
                return new WriteCellData<>(CODE_STATUS_1);
            }
            if (Objects.equals(CodeConstant.CODE_STATUS_2, value)) {
                return new WriteCellData<>(CODE_STATUS_2);
            }
            if (Objects.equals(CodeConstant.CODE_STATUS_3, value)) {
                return new WriteCellData<>(CODE_STATUS_3);
            }
            if (Objects.equals(CodeConstant.CODE_STATUS_4, value)) {
                return new WriteCellData<>(CODE_STATUS_4);
            }
        }
        // 出入库状态
        if (Objects.equals("storeStatus", fileName)) {
            if (Objects.equals(CodeConstant.STORE_STATUS_1, value)) {
                return new WriteCellData<>(CODE_STORE_STATUS_1);
            }
            if (Objects.equals(CodeConstant.STORE_STATUS_2, value)) {
                return new WriteCellData<>(CODE_STORE_STATUS_2);
            }
            if (Objects.equals(CodeConstant.STORE_STATUS_3, value)) {
                return new WriteCellData<>(CODE_STORE_STATUS_3);
            }
            if (Objects.equals(CodeConstant.STORE_STATUS_4, value)) {
                return new WriteCellData<>(CODE_STORE_STATUS_4);
            }
            if (Objects.equals(CodeConstant.STORE_STATUS_5, value)) {
                return new WriteCellData<>(CODE_STORE_STATUS_5);
            }
        }
        return new WriteCellData<>(val);
    }
}
